﻿/*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
 *~*  16-Oct-2009   www.Learning-Leisure.com
 *~*  Main Project : Facebook Membership Provider
 *~*  
 *~*  Project : PB.CustomRoleProvider    Namespace : PB.CustomRoleProvider
 *~*  File    : EnumBasedRoleProvider.cs    Class  : EnumBasedRoleProvider
 *~*  Author : Pinal Bhatt 
 *~*            self@P-Bhatt.com, www.P-Bhatt.com
 *~*   Version History
 *~*            Create Date         :     16-Oct-2009       Pinal Bhatt
 *~*           Freeze Date(Ver 1.0) :     23-Oct-2009       Pinal Bhatt 
 *~*            Last Update Date    :     23-Oct-2009       Pinal Bhatt  
 *~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/

using System;
using System.Web.Security;
using System.Collections.Specialized;

namespace PB.CustomRoleProvider
{
    /// <summary>
    /// Custom Role Provider beased on Enum
    /// </summary>
    public class EnumBasedRoleProvider : RoleProvider
    {
        #region Constants
        private const string Not_Supported_Error_Msg = "{0} is not supported in Enum Based Custom Provider";
        private const string Not_Implemented_Error_Msg = "{0} is not implemented in Enum Based Custom Provider under current scope";
        #endregion

        #region Overridden Members       

        public override string ApplicationName
        {
            get
            {
                return "FBMembership-Demo";
            }
            set
            {
                throw new NotImplementedException(string.Format(Not_Implemented_Error_Msg, "ApplicationName-Set"));
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "CreateRole"));
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotSupportedException(string.Format(Not_Supported_Error_Msg, "DeleteRole"));
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException(string.Format(Not_Implemented_Error_Msg, "FindUsersInRole"));
        }

        public override string[] GetAllRoles()
        {
            return Enum.GetNames(typeof(RoleEnum));
        }

        public override string[] GetRolesForUser(string username)
        {           
                return RoleHelper.GetRolesForUser(username);          
        }

        public override string[] GetUsersInRole(string roleName)
        {
            return RoleHelper.GetUsersInRole(roleName);
        }


        public override bool IsUserInRole(string username, string roleName)
        {
            return RoleHelper.IsUserInRole(username, roleName);
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            RoleHelper.RemoveUsersFromRoles(usernames, roleNames);
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            RoleHelper.AddUsersToRoles(usernames, roleNames);
        }

        public override bool RoleExists(string roleName)
        {
            if(roleName != null)
            {
                if (roleName.Trim().Length > 0)
                {
                    return Enum.IsDefined(typeof(RoleEnum), roleName);
                }
                else
                {
                    throw new ArgumentException("Parameter value cannot be empty string", "roleName");
                }
            }
            else
            {
                throw new ArgumentNullException("roleName", "Parameter cannot be null");
            }
        }

        #endregion

        #region Override Members from System.Configuration.Provider.ProviderBase

        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);
        }
        #endregion       

    }
}
